package vip.xiaonuo.controller.otherapi;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.controller.otherapi.vo.IdNameBar;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.memberuser.service.MemberUserService;
import vip.xiaonuo.sys.modular.org.entity.SysOrg;
import vip.xiaonuo.sys.modular.org.service.SysOrgService;

import java.util.ArrayList;
import java.util.List;

@RestController
public class UserApiController {

    @Autowired
    private SysOrgService orgService;

    @Autowired
    private MemberUserService memberUserService;

    @GetMapping("/userApi/v2.0/getUserOrgTreeData")
    public List<IdNameBar> getUserOrgTreeData(@RequestParam(defaultValue = "0") Long orgId) {
        LambdaQueryWrapper<SysOrg> orgMapper = new LambdaQueryWrapper<>();
        orgMapper.eq(SysOrg::getPid, orgId);
        List<IdNameBar> orgTreeVos = new ArrayList<>();
        orgService.list(orgMapper).forEach(org -> orgTreeVos.add(IdNameBar.builder()
                .id(org.getId())
                .name(org.getName())
                .type("dept")
                .build()));

        LambdaQueryWrapper<MemberUser> userMapper = new LambdaQueryWrapper<>();
        userMapper.eq(MemberUser::getOrgId,  orgId );
        memberUserService.list(userMapper).forEach(user -> orgTreeVos.add(IdNameBar.builder()
                .id(user.getId())
                .name(user.getName())
                .type("user")
                .build()));
        return orgTreeVos;
    }

    @GetMapping("/userApi/v2.0/getUserData")
    public List<IdNameBar> getUserData(@RequestParam String userName) {
        LambdaQueryWrapper<MemberUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(MemberUser::getName, "%" + userName + "%");
        List<IdNameBar> orgTreeVos = new ArrayList<>();
        memberUserService.list(lambdaQueryWrapper).forEach(user -> {
            orgTreeVos.add(IdNameBar.builder().type("user")
                    .name(user.getName())
                    .id(user.getId())
                    .build());
        });
        return orgTreeVos;
    }
}
